package com.nl.controller.web.controller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.nl.controller.web.bean.ChangeData;
import com.nl.controller.web.service.ChangeSynchronizationInterfaceService;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class SynMgr {
	
	private String getStreamSeq() {
        String streamSeq = changeSynchronizationInterfaceService.getStreamSeq();
        log.debug("获取流水序列号：{}", streamSeq);
        return streamSeq;
    }
	@Autowired
	private ChangeSynchronizationInterfaceService changeSynchronizationInterfaceService;
    //第一次延迟10秒后执行，之后按fixedRate的规则每300秒执行一次。000
    @Scheduled(initialDelay = 10000, fixedRate = 300000)
    public void syn() {
    	log.info("同步开始");
    	List<ChangeData> changeDatas = changeSynchronizationInterfaceService.findChange();
    	//log.info(changeDatas.size()+"");
    	if(!changeDatas.isEmpty()){
    	for (ChangeData changeData : changeDatas) {
    		JSONObject json = new JSONObject();
    		changeData.setStreamSeq(getStreamSeq());
   		 	//log.info(" 项目信息：{}", changeData.toString());
   		 	/*json.put("changeData",changeData);*/
   		 	json.put("mgrId",changeData.getMgrId());
   		 	json.put("checkState",changeData.getCheckState());
   		 	json.put("mgrState",changeData.getMgrState());
   		 	json.put("sendStart",changeData.getSendStart());
   		 	json.put("sendEnd",changeData.getSendEnd());
   		 	json.put("streamSeq",changeData.getStreamSeq());
   		 	json.put("flag","program_update");
   		 	//System.out.println("json:"+json.toString());
   		 	Map<String, Object> map = changeSynchronizationInterfaceService.changeSynchronization(json);
   		 	//log.info(" 定时任务返回信息：{}", map.toString());
		}
    	} 
		 
    }

}